import React, { Component, createContext } from "react";
import Child from "./Child";

export const { Provider, Consumer } = createContext();

class App extends Component {
  state = {
    count: 7,
  };

  add = () => {
    this.setState((prevState) => {
      return {
        count: prevState.count + 1,
      };
    });
  };

  minus = () => {
    this.setState((prevState) => {
      return {
        count: prevState.count - 1,
      };
    });
  };

  render() {
    return (
      <Provider
        value={{ count: this.state.count, add: this.add, minus: this.minus }}
      >
        <h2>context-计数器</h2>
        <Child />
      </Provider>
    );
  }
}

export default App;
